package com.wufun.overseas.main.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.wufun.baselibrary.download.CommonDownloadTask;
import com.wufun.overseas.R;
import com.wufun.overseas.adapters.SettingAdapter;
import com.wufun.overseas.common.SettingTypeInterface;
import com.wufun.overseas.main.activity.DownloadManagerActivity;
import com.wufun.overseas.main.activity.FaceTransferHomePageActivity;
import com.wufun.overseas.main.activity.FeedBackActivity;
import com.wufun.overseas.main.activity.MainActivity;
import com.wufun.overseas.main.activity.MoreSettingActivity;
import com.wufun.overseas.main.base.MyFragment;
import com.wufun.overseas.models.event.DownloadResultEvent;
import com.wufun.overseas.tools.DownloadManager;
import com.wufun.overseas.utils.IntentUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by tangbl on 2017/11/30 0030.
 */

public class SettingFragment extends MyFragment {

    @Bind(R.id.bar_title_tx)
    TextView barTitleTx;
    @Bind(R.id.setting_gv)
    GridView settingGv;

    private SettingAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container
            , @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_setting, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void fragmentEvent(DownloadResultEvent eventData) {
        super.fragmentEvent(eventData);
        if (eventData.status != CommonDownloadTask.STATUS_PROGRESS) {
            checkShow();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        checkShow();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser)
            checkShow();
    }

    @Override
    public void initWidget(View... v) {
        super.initWidget(v);

        barTitleTx.setText(R.string.setting_title_str);

        mAdapter = new SettingAdapter(getActivity());
        settingGv.setAdapter(mAdapter);
        settingGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int type = mAdapter.getItem(position).getType();
                String title = mAdapter.getItem(position).getTitle();
                switch (type) {
                    case SettingTypeInterface.TYPE_TRANSFER:
                        startActivity(FaceTransferHomePageActivity.buildIntent(getActivity(), title));
                        break;
                    case SettingTypeInterface.TYPE_GAMEPAD:
                        IntentUtil.getInstance().goJoystickManager(getActivity());
                        break;
                    case SettingTypeInterface.TYPE_DOWNLOAD:
                        startActivity(DownloadManagerActivity.buildIntent(getActivity(), title));
                        break;
                    case SettingTypeInterface.TYPE_FEEDBACK:
                        startActivity(FeedBackActivity.buildIntent(getActivity(), title));
                        break;
                    case SettingTypeInterface.TYPE_MORE:
                        startActivity(MoreSettingActivity.buildIntent(getActivity(), title));
                        break;
                }
            }
        });
    }

    private void checkShow() {
        mAdapter.setShow(DownloadManager.getInstance().getAllNotFinishDownload());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.bar_back_iv, R.id.bar_back_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bar_back_iv:
            case R.id.bar_back_ll:
                ((MainActivity) getActivity()).selectItemView(0);
                break;
        }
    }
}
